

import random
import requests
import time

from db import *
from spider_xue.ip import IP, headers_list
# from spider_xue.settings import COOKIES_LIST
from spider_xue.utils import send_msg


accounts = [
    {
        "phone": "chrome_home",
        "cookies": "device_id=6c5a10e81d7b783ef6a341ca8b380308; s=bt14kl7km6; cookiesu=851630631262884; bid=866002b3dfd182def1b13ed70b5b581c_kte0t70t; xq_a_token=737318e45e37283c174e6861710dab688eebe737; xqat=737318e45e37283c174e6861710dab688eebe737; xq_r_token=396a59ce874f08fc58c8d546dffacc59ee1b372b; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTYzNDU3ODI5NCwiY3RtIjoxNjMyMjcyODQ2NTc0LCJjaWQiOiJkOWQwbjRBWnVwIn0.MC71K0WfjR6WZcqGjmSrCcMY0NoqJTfvtaq4v89DyX6jOR-7aoOzbRYvJ2iun3RbR-uG10Rw_wtJxFWiSslFWuuFP5ojIxDqwfoEx7iE3LIy5jU2YEZ4N_RP1UegMdgZC6zQBHz8RehSzV0fkQH10y6VBr9XITm4p61WQD0I6Ic1vyZ_uPuNnrkTq90C3kRl3edI8_8fHlSZwiX5LpDhFWmHzfq1p9CxJKneda3O0dM3CCCT34McuDBnh56N8viOBzXNzYWCaJxRAAQAZ6by6A1G6j7wvslmQCtXeTKdJCDcTuY6HbhQmVT4f-OI8eSvuqM8ktarG34YbPL0V0FoDA; u=571632272905562; Hm_lvt_1db88642e346389874251b5a1eded6e3=1631787177,1631845889,1631932345,1632272910; is_overseas=0; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1632272946",
        "use": 1,
        "is": 0,
    }
]


def test(account):
    cookie = account['cookies']
    time.sleep(1)
    tt = str(int(time.time())) + "000"
    # tt = "1631602800" + "000"
    url = "https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=SH600096&begin={}&period=5m&type=before&count=-1000&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance"
    ss = requests.Session()
    ss.proxies = {"http": "http://{}:{}".format('206.253.164.101', '80')}
    # ss.cookies = {}
    ss.headers = {"user-agent": random.choice(headers_list), "cookie": cookie}
    html = ss.get(url.format(tt))
    if html.status_code != 200:
        return False
    if not html.json()["data"]:
        return False
    t = html.json()["data"]
    return True


def saveAccount():
    count = []
    dele = []
    accounts = account_db.find()
    for i in accounts:
        if test(i):
            account_db.update_many({'phone': i['phone']}, {
                                   "$setOnInsert": i}, upsert=True)
            count.append(i['phone'])
        else:
            print("delete %s ..........." % i['phone'])
            # send_msg("### cookies shixiaole %s" % i['phone'])
            dele.append(i['phone'])
    print("all_survive is : %s, delete is: %s : %s" %
          (len(count), len(dele), ','.join(dele)))


def set_account_is_yes(account):

    account_db.update_one({'phone': account['phone']}, {"$set": {"is": 1}})


def set_account_is_not(account):

    account_db.update_one({'phone': account['phone']}, {"$set": {"is": 0}})


def set_account_use_yes(account):

    account_db.update_one({'phone': account['phone']}, {"$set": {"use": 1}})


def set_account_use_not(account):

    account_db.update_one({'phone': account['phone']}, {"$set": {"is": 0}})


def set_account(account):

    account_db.update_one({'phone': account['phone']}, {"$set": account})


def update_account():
    account_db.delete_many({"use": 1})
    tmp = []
    count = 0
    for i in range(10):
        for x in COOKIES_LIST:
            d = {
                "phone": f"chrome_x{count}",
                "cookies": x,
                "use": 1,
                "is": 0,
            }
            tmp.append(d)
            count += 1
    account_db.insert_many(tmp)


if __name__ == '__main__':
    saveAccount(accounts)
    # account = accounts[-1]
    # test(account)
